package com.scc.mapper;

import com.scc.pojo.Emp;
import com.scc.pojo.Message;
import com.scc.pojo.MessageList;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface MessageMapper {

    //添加消息列表
    @Insert("insert into messagelist (fromEmpId, toEmpId) value (#{fromEmpId},#{toEmpId})")
    void insertMessageList(MessageList messageList);

    //我发起的聊天
    @Select("select messagelist.id,messagelist.fromEmpId,messagelist.toEmpId,emp.account,emp.image,messagelist.messageNumber from messagelist " +
            "join emp on messagelist.toEmpId = emp.id where fromEmpId = #{fromEmpId}")
    List<MessageList> selectMyMessageList(Integer fromEmpId);


    //别人发起的聊天
    @Select("select messagelist.id,messagelist.fromEmpId,messagelist.toEmpId,emp.account,emp.image,messagelist.messageNumber from messagelist " +
            "join emp on messagelist.fromEmpId = emp.id where toEmpId = #{toEmpId}")
    List<MessageList> selectOtherMessageList(Integer toEmpId);

    //删除好友之后消息列表也要删除
    @Delete("delete from messagelist where fromEmpId = #{applyId}")
    void deleteMessageListByApplyId(Integer applyId);

    //要保存当前员工发送的消息
    @Insert("insert into message (fromEmpId, toEmpId, message, messageId) value " +
            "(#{fromEmpId},#{toEmpId},#{message},#{messageId})")
    void insertMessage(Message msg);

    //获取历史聊天记录根据消息的id标记
    @Select("select * from message where messageId = #{id}")
    List<Message> getHistoryMessageByMessageId(Integer id);

    //查询fromEmpId的对象数据
    @Select("select * from emp where id = #{fromEmpId}")
    Emp selectEmpByFromEmpId(Integer fromEmpId);

    //查询toEmpI对应的员工
    @Select("select * from emp where id = #{toEmpId}")
    Emp selectEmpByToEmpId(Integer toEmpId);

    //对应messageList消息数量要加一
    @Update("update messagelist set messageNumber = messageNumber + 1 where id = #{messageId}")
    void addMessageNumberById(Integer messageId);

    //删除聊天记录
    @Delete("delete from message where messageId = #{id}")
    void deleteMessageList(Integer id);

    //聊天数量清空
    @Update("update messagelist set messageNumber = 0 where id = #{id}")
    void updateMessageNumber(Integer id);
}
